/*
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

/*
 *
 *
 *
 *
 *
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

package java.util.concurrent;

/**
 * An {@link ExecutorService} that can schedule commands to run after a given
 * delay, or to execute periodically.
 *
 * <p>The {@code schedule} methods create tasks with various delays
 * and return a task object that can be used to cancel or check
 * execution. The {@code scheduleAtFixedRate} and
 * {@code scheduleWithFixedDelay} methods create and execute tasks
 * that run periodically until cancelled.
 *
 * <p>Commands submitted using the {@link Executor#execute(Runnable)}
 * and {@link ExecutorService} {@code submit} methods are scheduled
 * with a requested delay of zero. Zero and negative delays (but not
 * periods) are also allowed in {@code schedule} methods, and are
 * treated as requests for immediate execution.
 *
 * <p>All {@code schedule} methods accept <em>relative</em> delays and
 * periods as arguments, not absolute times or dates. It is a simple
 * matter to transform an absolute time represented as a {@link
 * java.util.Date} to the required form. For example, to schedule at
 * a certain future {@code date}, you can use: {@code schedule(task,
 * date.getTime() - System.currentTimeMillis(),
 * TimeUnit.MILLISECONDS)}. Beware however that expiration of a
 * relative delay need not coincide with the current {@code Date} at
 * which the task is enabled due to network time synchronization
 * protocols, clock drift, or other factors.
 *
 * <p>The {@link Executors} class provides convenient factory methods for
 * the ScheduledExecutorService implementations provided in this package.
 *
 * <h3>Usage Example</h3>
 *
 * Here is a class with a method that sets up a ScheduledExecutorService
 * to beep every ten seconds for an hour:
 *
 *  <pre> {@code
 * import static java.util.concurrent.TimeUnit.*;
 * class BeeperControl {
 *   private final ScheduledExecutorService scheduler =
 *     Executors.newScheduledThreadPool(1);
 *
 *   public void beepForAnHour() {
 *     final Runnable beeper = new Runnable() {
 *       public void run() { System.out.println("beep"); }
 *     };
 *     final ScheduledFuture<?> beeperHandle =
 *       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
 *     scheduler.schedule(new Runnable() {
 *       public void run() { beeperHandle.cancel(true); }
 *     }, 60 * 60, SECONDS);
 *   }
 * }}</pre>
 *
 * @since 1.5
 * @author Doug Lea
 */
// 一个ExecutorService，它可以安排命令在给定的延迟后运行，或定期执行。
// schedule方法创建具有各种延迟的任务，并返回可用于取消或检查执行的任务对象。
// scheduleAtFixedRate和scheduleWithFixedDelay方法创建并执行周期性运行的任务，直到被取消。
// 使用Executor.execute(Runnable)和ExecutorService提交方法提交的命令被调度为请求延迟为零
// 。在调度方法中也允许出现零延迟和负延迟(但不允许出现周期)，它们被视为立即执行的请求。
// 所有调度方法都接受相对延迟和周期作为参数，而不是绝对时间或日期。将表示为java.util.Date的绝对时间转换为所需的形式是一件简单的事情。
// 例如，要计划未来的某个日期，您可以使用:
// schedule(task, date. gettime () - System.currentTimeMillis()， TimeUnit.MILLISECONDS)。
// 但是要注意，由于网络时间同步协议、时钟漂移或其他因素，相对延迟的过期时间不需要与任务启用的当前日期一致。
// Executors类为这个包中提供的ScheduledExecutorService实现提供了方便的工厂方法。
// 使用的例子 下面是一个类，它的方法设置了一个ScheduledExecutorService，在一个小时内每十秒发出一次beep声:
// import static java.util.concurrent.TimeUnit.*;
// class BeeperControl {
//     private final ScheduledExecutorService scheduler =
//             Executors.newScheduledThreadPool(1);
//     public void beepForAnHour() {
//         final Runnable beeper = new Runnable() {
//             public void run() { System.out.println("beep"); }
//         };
//         final ScheduledFuture<?> beeperHandle =
//                 scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
//         scheduler.schedule(new Runnable() {
//             public void run() { beeperHandle.cancel(true); }
//         }, 60 * 60, SECONDS);
//     }
// }
public interface ScheduledExecutorService extends ExecutorService {

    /**
     * Creates and executes a one-shot action that becomes enabled
     * after the given delay.
     *
     * @param command the task to execute
     * @param delay the time from now to delay execution
     * @param unit the time unit of the delay parameter
     * @return a ScheduledFuture representing pending completion of
     *         the task and whose {@code get()} method will return
     *         {@code null} upon completion
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if command is null
     */
    // 创建并执行在给定延迟后启用的一次性操作。
    public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);

    /**
     * Creates and executes a ScheduledFuture that becomes enabled after the
     * given delay.
     *
     * @param callable the function to execute
     * @param delay the time from now to delay execution
     * @param unit the time unit of the delay parameter
     * @param <V> the type of the callable's result
     * @return a ScheduledFuture that can be used to extract result or cancel
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if callable is null
     */
    // 创建并执行在给定延迟后启用的ScheduledFuture。
    // 参数: 可调用的——要执行的函数
    // 延迟-从现在开始延迟执行的时间
    // Unit -延迟参数的时间单位
    // 类型参数:  -可调用对象结果的类型
    // 返回: 可用于提取结果或取消的ScheduledFuture
    // 抛出:
    // RejectedExecutionException -如果任务不能被调度执行
    // NullPointerException -如果callable为空
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

    /**
     * Creates and executes a periodic action that becomes enabled first
     * after the given initial delay, and subsequently with the given
     * period; that is executions will commence after
     * {@code initialDelay} then {@code initialDelay+period}, then
     * {@code initialDelay + 2 * period}, and so on.
     * If any execution of the task
     * encounters an exception, subsequent executions are suppressed.
     * Otherwise, the task will only terminate via cancellation or
     * termination of the executor.  If any execution of this task
     * takes longer than its period, then subsequent executions
     * may start late, but will not concurrently execute.
     *
     * @param command the task to execute
     * @param initialDelay the time to delay first execution
     * @param period the period between successive executions
     * @param unit the time unit of the initialDelay and period parameters
     * @return a ScheduledFuture representing pending completion of
     *         the task, and whose {@code get()} method will throw an
     *         exception upon cancellation
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if command is null
     * @throws IllegalArgumentException if period less than or equal to zero
     */
    // 创建并执行一个周期操作，该操作在给定的初始延迟后首先启用，然后在给定的周期内启用;
    // 也就是说，执行将在initialDelay之后开始，然后是initialDelay+period，然后是initialDelay+ 2 * period，等等。
    // 如果任务的任何执行遇到异常，则抑制后续执行。否则，任务只会通过取消或终止执行程序而终止。
    // 如果此任务的任何执行时间长于其周期，则后续执行可能会延迟开始，但不会并发执行。
    // 参数: 命令—要执行的任务
    // initialDelay -延迟第一次执行的时间
    // 周期-连续执行之间的周期
    // unit - initialDelay和period参数的时间单位
    // 返回: 表示待完成任务的ScheduledFuture，其get()方法将在取消时抛出异常
    // 抛出: RejectedExecutionException -如果任务不能被调度执行
    // NullPointerException - if命令为空
    // IllegalArgumentException -如果周期小于等于零
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);

    /**
     * Creates and executes a periodic action that becomes enabled first
     * after the given initial delay, and subsequently with the
     * given delay between the termination of one execution and the
     * commencement of the next.  If any execution of the task
     * encounters an exception, subsequent executions are suppressed.
     * Otherwise, the task will only terminate via cancellation or
     * termination of the executor.
     *
     * @param command the task to execute
     * @param initialDelay the time to delay first execution
     * @param delay the delay between the termination of one
     * execution and the commencement of the next
     * @param unit the time unit of the initialDelay and delay parameters
     * @return a ScheduledFuture representing pending completion of
     *         the task, and whose {@code get()} method will throw an
     *         exception upon cancellation
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if command is null
     * @throws IllegalArgumentException if delay less than or equal to zero
     */
    // 创建并执行一个周期性操作，该操作首先在给定的初始延迟后启用，然后在一个执行的终止和下一个执行的开始之间使用给定的延迟。
    // 如果任务的任何执行遇到异常，则抑制后续执行。
    // 否则，任务只会通过取消或终止执行程序而终止。
    // 参数:
    // 命令—要执行的任务
    // initialDelay -延迟第一次执行的时间
    // 延迟-一个执行的终止和下一个执行的开始之间的延迟
    // unit - initialDelay和delay参数的时间单位
    // 返回: 表示待完成任务的ScheduledFuture，其get()方法将在取消时抛出异常
    // 抛出: RejectedExecutionException -如果任务不能被调度执行
    // NullPointerException - if命令为空 IllegalArgumentException -如果延迟小于或等于零
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

}
